{% extends "base.html" %}

{% block title %}编辑学生{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-8 offset-md-2">
        <h2 class="mb-4">编辑学生</h2>
        <form method="POST">
            <div class="mb-3">
                <label for="name" class="form-label">姓名</label>
                <input type="text" class="form-control" id="name" name="name" value="{{ student.name }}" required>
            </div>
            <div class="mb-3">
                <label for="gender" class="form-label">性别</label>
                <select class="form-control" id="gender" name="gender" required>
                    <option value="男" {% if student.gender == '男' %}selected{% endif %}>男</option>
                    <option value="女" {% if student.gender == '女' %}selected{% endif %}>女</option>
                </select>
            </div>
            <div class="mb-3">
                <label for="birth_date" class="form-label">出生日期</label>
                <input type="date" class="form-control" id="birth_date" name="birth_date" value="{{ student.birth_date }}" required>
            </div>
            <div class="mb-3">
                <label for="id_card" class="form-label">身份证号</label>
                <input type="text" class="form-control" id="id_card" name="id_card" value="{{ student.id_card }}" required>
            </div>
            <div class="mb-3">
                <label for="address" class="form-label">家庭住址</label>
                <input type="text" class="form-control" id="address" name="address" value="{{ student.address }}" required>
            </div>
            <div class="mb-3">
                <label for="parent_id" class="form-label">选择家长</label>
                <div class="input-group">
                    <select class="form-control" id="parent_id" name="parent_id">
                        <option value="">新建家长</option>
                        {% for parent in parents %}
                        <option value="{{ parent.id }}" {% if student.parent_id == parent.id %}selected{% endif %}>
                            {{ parent.name }} ({{ parent.phone }})
                        </option>
                        {% endfor %}
                    </select>
                    <a href="{{ url_for('add_parent') }}" class="btn btn-primary" target="_blank">添加家长</a>
                </div>
            </div>
            <div id="new_parent_fields" style="display: none;">
                <div class="mb-3">
                    <label for="parent_name" class="form-label">家长姓名</label>
                    <input type="text" class="form-control" id="parent_name" name="parent_name">
                </div>
                <div class="mb-3">
                    <label for="phone" class="form-label">联系电话</label>
                    <input type="tel" class="form-control" id="phone" name="phone">
                </div>
            </div>
            <div class="mb-3">
                <label for="parent_relation" class="form-label">家长关系</label>
                <select class="form-control" id="parent_relation" name="parent_relation" required>
                    <option value="爸爸" {% if student.parent_relation == '爸爸' %}selected{% endif %}>爸爸</option>
                    <option value="妈妈" {% if student.parent_relation == '妈妈' %}selected{% endif %}>妈妈</option>
                    <option value="爷爷" {% if student.parent_relation == '爷爷' %}selected{% endif %}>爷爷</option>
                    <option value="奶奶" {% if student.parent_relation == '奶奶' %}selected{% endif %}>奶奶</option>
                    <option value="外公" {% if student.parent_relation == '外公' %}selected{% endif %}>外公</option>
                    <option value="外婆" {% if student.parent_relation == '外婆' %}selected{% endif %}>外婆</option>
                    <option value="其他" {% if student.parent_relation == '其他' %}selected{% endif %}>其他</option>
                </select>
            </div>
            <div class="mb-3">
                <button type="submit" class="btn btn-primary">保存</button>
                <a href="{{ url_for('list_students') }}" class="btn btn-secondary">返回</a>
            </div>
        </form>
    </div>
</div>

{% block scripts %}
<script>
document.getElementById('parent_id').addEventListener('change', function() {
    var newParentFields = document.getElementById('new_parent_fields');
    var parentNameInput = document.getElementById('parent_name');
    var phoneInput = document.getElementById('phone');
    
    if (this.value === '') {
        newParentFields.style.display = 'block';
        parentNameInput.required = true;
        phoneInput.required = true;
    } else {
        newParentFields.style.display = 'none';
        parentNameInput.required = false;
        phoneInput.required = false;
    }
});

// 初始化时触发一次
document.getElementById('parent_id').dispatchEvent(new Event('change'));
</script>
{% endblock %}
{% endblock %} 